<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css"  media="all">
    <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
    <link rel="stylesheet" href="__CSS__/admin.css"  media="all">
    <style type="text/css">
        /* tooltip */
        #tooltip{
            position:absolute;
            border:1px solid #ccc;
            background:#333;
            padding:2px;
            display:none;
            color:#fff;
        }
    </style>
</head>
<body style="padding:10px;">
<div class="tplay-body-div">
    <div class="layui-tab">
        <ul class="layui-tab-title">
            <li class="layui-this">发放补助</li>
            <?php if(!in_array($admin_cate_id,$operation)){ ?>
            <li><a href="{:url('admin/subsidy/subsidy')}" class="a_menu">补助</a></li>
            <?php } ?>
        </ul>
    </div>
    <h1 align="center">{$zhu.name}的发放金额</h1>
    <br /><br />
    <?php if(!in_array($admin_cate_id,$operation)){ ?>
    <div style="text-align:center"><a class="layui-btn add" >增加金额</a></div>
    <?php } ?>
    <table class="layui-table" id="table" lay-filter="test1" lay-size="lg">
        <colgroup>
            <col width="180">
            <col width="180">
            <col width="130">
            <col width="150">
            <col width="300">
            <col width="150">
            <col width="300">
            <col width="150">
        </colgroup>
        <thead>

        <tr>
            <th>取暖费</th>
            <th>租房费</th>
            <th>生活补助</th>
            <th>搬家补助</th>
            <th>其它1</th>
            <th>备注</th>
            <th>年份</th>
            <?php if(!in_array($admin_cate_id,$operation)){ ?>
            <th>操作</th>
            <?php } ?>

        </tr>
        </thead>
        <tbody>
        {volist name="list" id="vo"}
        <tr>
            <td>{$vo.qunuanfei}</td>
            <td>{$vo.zufangfei}</td>
            <td>{$vo.shenghuobu}</td>
            <td>{$vo.banjiabu}</td>
            <td>{$vo.qita1}</td>
            <td>{$vo.bz}</td>
            <td>{$vo.year}</td>

            <td>
                <?php if(!in_array($admin_cate_id,$operation)){ ?>
                <a href="javascript:;" class="layui-btn layui-btn-xs layui-btn-primary delete" id="{$vo.id}" style="margin-right: 0;font-size:12px;"><i class="layui-icon"></i></a>
                <?php } ?>
                <a onclick="edit('{$vo.id}')" class="layui-btn layui-btn-xs a_menu layui-btn-primary" style="margin-right: 0;font-size:12px;"><i class="layui-icon"></i></a>

            </td>

        </tr>
        {/volist}

        <tr>
            <td>总计：</td>
            <td colspan="7"><input value="{$allMoney}" id="sum" name="sum" readonly type="text"></td>
        </tr>
        </tbody>
    </table>
    <div style="padding:0 20px;"></div>
    {include file="public/foot"}
</div>

<script>

    //因为动态添加的元素class属性是无效的，所以不能用$('.add').click(function(){});
    $('body').on('click', '.add', function() {
        $('.add').hide();
        //你要添加的html
        var html = '<tr><form id="aaa">'+
                '<td><input type="text" class="layui-input" name="qunuanfei"></td>'+
                '<td><input type="text" class="layui-input" name="zufangfei"></td>'+
                '<td><input type="text" class="layui-input" name="shenghuobu"></td>'+
                '<td><input type="text" class="layui-input" name="banjiabu"></td>'+
                '<td><input type="text" class="layui-input" name="qita1"></td>'+
                '<td><input type="text" class="layui-input" name="bz"></td>'+
                '<td><select id="year" name="year" lay-filter="first" id="first" lay-filter="aihao"><option value="">=请选择=</option> <option value="2019">2019</option><option value="2020">2020</option><option value="2021">2021</option></select></td></form>'+

                "<td>"+
                "<a class='layui-btn layui-btn-xs edit data-count-edit' lay-event='edit'>保存数据</a><a class='layui-btn del layui-btn-xs layui-btn-danger data-count-delete' lay-event='delete'>删除</a>"+
                "</td>"+
                '</tr>';
        // // console.log(html)
        // //添加到表格最后
        $(html).appendTo($('#table tbody:last'));
        // $(".layui-table-body .layui-table").append(html);
        // form.render();//因为有select元素，所有添加后要重新渲染一次
    });
    $('body').on('click', '.del', function() {
        if ($('#table tbody tr').length === 1) {
            layer.msg('只有一条不允许删除。', {
                time : 2000
            });
        } else {
            $(this).closest('tr').remove();
            $('.add').show();
        }
    });


    $("input[name=qunuanfei]").blur(function(){
        var value1= $('input[name=qunuanfei]').val();
        var value2= $('input[name=zufangfei]').val();
        var value3= $('input[name=shenghuobu]').val();
        var value4= $('input[name=banjiabu]').val();
        var value5= $('input[name=qita1]').val();
        var sumValue = parseFloat(value1)+parseFloat(value2)+parseFloat(value3)+parseFloat(value4)+parseFloat(value5);
        if(sumValue>0){
            $('#sum').val(parseInt(sumValue));
        }
        else{
            $('#sum').val(0);
        }

    })

    $("input[name=zufangfei]").blur(function(){
        var value1= $('input[name=qunuanfei]').val();
        var value2= $('input[name=zufangfei]').val();
        var value3= $('input[name=shenghuobu]').val();
        var value4= $('input[name=banjiabu]').val();
        var value5= $('input[name=qita1]').val();
        var sumValue = parseFloat(value1)+parseFloat(value2)+parseFloat(value3)+parseFloat(value4)+parseFloat(value5);
        if(sumValue>0){
            $('#sum').val(parseInt(sumValue));
        }
        else{
            $('#sum').val(0);
        }

    })

    $("input[name=shenghuobu]").blur(function(){
        var value1= $('input[name=qunuanfei]').val();
        var value2= $('input[name=zufangfei]').val();
        var value3= $('input[name=shenghuobu]').val();
        var value4= $('input[name=banjiabu]').val();
        var value5= $('input[name=qita1]').val();
        var sumValue = parseFloat(value1)+parseFloat(value2)+parseFloat(value3)+parseFloat(value4)+parseFloat(value5);
        if(sumValue>0){
            $('#sum').val(parseInt(sumValue));
        }
        else{
            $('#sum').val(0);
        }

    })


    $("input[name=banjiabu]").blur(function(){
        var value1= $('input[name=qunuanfei]').val();
        var value2= $('input[name=zufangfei]').val();
        var value3= $('input[name=shenghuobu]').val();
        var value4= $('input[name=banjiabu]').val();
        var value5= $('input[name=qita1]').val();
        var sumValue = parseFloat(value1)+parseFloat(value2)+parseFloat(value3)+parseFloat(value4)+parseFloat(value5);
        if(sumValue>0){
            $('#sum').val(parseInt(sumValue));
        }
        else{
            $('#sum').val(0);
        }

    })


    $("input[name=qita1]").blur(function(){
        var value1= $('input[name=qunuanfei]').val();
        var value2= $('input[name=zufangfei]').val();
        var value3= $('input[name=shenghuobu]').val();
        var value4= $('input[name=banjiabu]').val();
        var value5= $('input[name=qita1]').val();
        var sumValue = parseFloat(value1)+parseFloat(value2)+parseFloat(value3)+parseFloat(value4)+parseFloat(value5);

        if(sumValue>0){
            $('#sum').val(parseInt(sumValue));
        }
        else{
            $('#sum').val(0);
        }

    })

    $(document).on('click','.edit',function(){
        var value1= $('input[name=qunuanfei]').val();
        var value2= $('input[name=zufangfei]').val();
        var value3= $('input[name=shenghuobu]').val();
        var value4= $('input[name=banjiabu]').val();
        var value5= $('input[name=qita1]').val();
        var sumValue = parseFloat(value1)+parseFloat(value2)+parseFloat(value3)+parseFloat(value4)+parseFloat(value5);

        $.ajax({
                    url:'{:url("admin/subsidy/info")}',
                    async : false,    //表示请求是否异步处理
                    type : "post",    //请求类型
                    dataType : "json",//返回的数据类型
                    data:{
                        'uid':{$zhu.id},
                    'qunuanfei':$("input[name='qunuanfei']").val(),
                    'zufangfei':$("input[name='zufangfei']").val(),
                    'shenghuobu':$("input[name='shenghuobu']").val(),
                    'banjiabu':$("input[name='banjiabu']").val(),
                    'qita1':$("input[name='qita1']").val(),
                    'bz':$("input[name='bz']").val(),
                    'year':$("#year").val(),
                     'sum': sumValue,
                },
                success:function (res) {
            if (res == 200) {
                layer.msg('操作成功', {
                    time : 2000
                });
                setTimeout(function(){
                    window.location.reload()
                },2000);

            }
        },
        error:function (err) {
            console.log(err);
        }
    })
    })

    $('.delete').click(function(){
        var id = $(this).attr('id');
        layer.confirm('确定要删除?', function(index) {
            $.ajax({
                url:"{:url('admin/subsidy/delete')}",
                data:{id:id},
                success:function(res) {
                    if(res.code == 1) {
                        layer.msg(res.msg);
                        setTimeout(function(){
                            location.reload();
                        },1500)

                    }else{
                        layer.msg(res.msg,{icon:1,time:3000});
                    }
                }
            })
        })
    })

    function edit(id){
        layer.open({
            type: 2,
            area: [600+'px', 600 +'px'],
            fix: false, //不固定
            maxmin: true,
            shadeClose: true,
            title: "编辑",
            content: "/admin/subsidy/edit.shtml?id=+"+id
        })
    }
</script>
</body>
</html>